package org.ithang.application.grade;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.ithang.application.customer.bean.Customer;
import org.ithang.application.grade.bean.Grade;
import org.ithang.application.grade.service.GradeService;
import org.ithang.application.operationLog.bean.OperationLog;
import org.ithang.application.operationLog.service.OperationLogService;
import org.ithang.system.user.bean.User;
import org.ithang.tools.lang.DateUtils;
import org.ithang.tools.lang.JsonUtils;
import org.ithang.tools.lang.MacUtil;
import org.ithang.tools.model.Action;
import org.ithang.tools.model.ActionResult;
import org.ithang.tools.model.ActionValues;
import org.ithang.tools.model.ErrorInfo;
import org.ithang.tools.model.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 分红档次
 * @author zyt 控制器
 *
 */
@Controller
@RequestMapping("/app/grade")
public class GradeAction extends Action<Grade>{

    @Autowired
    private GradeService gradeService; 

    @Autowired
    private OperationLogService optService;
    
    @ResponseBody
	@RequestMapping(value="add",method=RequestMethod.POST)
	public ActionResult add(Grade grade,HttpServletRequest request){
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		User user=(User)subject.getPrincipal();
    		if(null!=user){
    			OperationLog log=new OperationLog();
    			log.setCreate_time(DateUtils.getSystime());
    			log.setUser_id(user.getId());
    			log.setUname(user.getUname());
    			StringBuilder sber=new StringBuilder("新增");
    			if(0==grade.getType()){
    				sber.append("加速分润档次");
    			}
    			if(1==grade.getType()){
    				sber.append("动态分润档次");
    			}
    			sber.append(" : 数据为 :").append(JsonUtils.toJsonStr(grade));
    			log.setContent(sber.toString());
    			log.setIp(MacUtil.getUserIP(request));
    			optService.add(log);
    		}
    		gradeService.add(grade);        	
        }else{
        	return error(ErrorInfo.NoLoginError);
        }
		
		return success();
	}
	
	 @RequestMapping("form")
    public String form(@RequestParam(value="id",required=false)Integer id,Model m){
    	if(null!=id){
    		Grade bean=gradeService.get(id);
    		m.addAttribute(bean);
    	}else{
    		m.addAttribute(new Grade());
    	}
    	return "app/grade/form";
    }
	 
	 @RequestMapping("sform")
	    public String sform(@RequestParam(value="id",required=false)Integer id,Model m){
	    	if(null!=id){
	    		Grade bean=gradeService.get(id);
	    		m.addAttribute(bean);
	    	}else{
	    		m.addAttribute(new Grade());
	    	}
	    	return "app/grade/sform";
	    }
	
	
	@ResponseBody
	@RequestMapping(value="update",method=RequestMethod.POST)
	public ActionResult update(Grade grade,HttpServletRequest request){
		
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		User user=(User)subject.getPrincipal();
    		if(null!=user){
    			
    			OperationLog log=new OperationLog();
    			log.setCreate_time(DateUtils.getSystime());
    			log.setUser_id(user.getId());
    			log.setUname(user.getUname());
    			StringBuilder sber=new StringBuilder("修改");
    			if(null!=grade&&0==grade.getType()){
    				sber.append("加速分润档次");
    			}
    			if(null!=grade&&1==grade.getType()){
    				sber.append("动态分润档次");
    			}
    			Grade preGrade=gradeService.get(grade.getId());
    			sber.append(" : 修改前数据为 :").append(JsonUtils.toJsonStr(preGrade));
    			sber.append(" : 修改后数据为 :").append(JsonUtils.toJsonStr(grade));
    			log.setContent(sber.toString());
    			log.setIp(MacUtil.getUserIP(request));
    			optService.add(log);
    		}
    		gradeService.update(grade);     	
        }else{
        	return error(ErrorInfo.NoLoginError);
        }
		
		
		return success();
	}


    @ResponseBody
	@RequestMapping(value="get",method=RequestMethod.GET)
	public ActionResult get(Integer id){
		Grade r=gradeService.get(id);
		return success(r);
	}
	
	@ResponseBody
	@RequestMapping(value="delete",method=RequestMethod.POST)
	public ActionResult delete(@RequestParam(value="id",required=false)Integer id,@RequestParam(value="ids",required=false)String ids,HttpServletRequest request){
		
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		User user=(User)subject.getPrincipal();
    		if(null!=user){
    			OperationLog log=new OperationLog();
    			log.setCreate_time(DateUtils.getSystime());
    			log.setUser_id(user.getId());
    			log.setUname(user.getUname());
    			StringBuilder sber=new StringBuilder("删除");
    			
    			if(null!=id){
    				Grade preGrade=gradeService.get(id);
    				if(0==preGrade.getType()){
        				sber.append("加速分润档次");
        			}
        			if(1==preGrade.getType()){
        				sber.append("动态分润档次");
        			}
        			
        			sber.append(" : 数据为 :").append(JsonUtils.toJsonStr(preGrade));
    			}
    			
    			if(null!=ids&&ids.length()>0){
    				for(String _id:ids.split(",")){
    					Grade grade=gradeService.get(Integer.valueOf(_id));
    					if(null!=grade){
    						sber.append(" : 数据为 :").append(JsonUtils.toJsonStr(grade));		
    			   		}
    				}
    			}
    			
    			log.setContent(sber.toString());
    			log.setIp(MacUtil.getUserIP(request));
    			optService.add(log);
    		}
    		if(null!=id&&id>0){
    			gradeService.delete(id);	
    		}
    		
    		if(null!=ids&&ids.length()>0){
    			gradeService.batchDelete(ids.split(","));
    		}     	
        }else{
        	return error(ErrorInfo.NoLoginError);
        }
		
		
		
		
		return success();
	}
	
	@ResponseBody
	@RequestMapping(value="list",method={RequestMethod.GET,RequestMethod.POST})
	public ActionResult list(Integer... ids){
		return success(gradeService.list(ids));
	}
	
	@ResponseBody
	@RequestMapping(value="page",method=RequestMethod.POST)
	public ActionResult page(Grade grade,Pager<Grade> page){
	    page.setBean(grade);
	    List<Grade> data=gradeService.page(page);
	    page.setData(data);
		return success(page);
	}
	
	@ResponseBody
	@RequestMapping(value="query",method=RequestMethod.POST)
	public ActionResult query(HttpServletRequest request){
		ActionValues values=new ActionValues(request);
		return success(gradeService.query(values));
	}
     

}